package com.example.onlineeducation.utils.pictureseletor

import android.app.Activity
import android.content.Context
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.os.Build
import androidx.core.content.ContextCompat
import com.example.onlineeducation.R
import com.luck.picture.lib.PictureSelectionModel
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType

import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.language.LanguageConfig
import com.luck.picture.lib.listener.OnResultCallbackListener
import com.luck.picture.lib.style.PictureParameterStyle


/*
* 图片选择器Utils
* */

object YcPictureSelecorUtil {

    //调用图片选择器 单选图片
    fun addSinglePicture(activity: Activity, listener: OnResultCallbackListener<LocalMedia>) {
        PictureSelector.create(activity)
            .openGallery(PictureMimeType.ofImage()) // 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
            .defaultSetting(activity = activity).apply {
                selectionMode(PictureConfig.SINGLE) // 多选 or 单选
                isSingleDirectReturn(true)
                imgFormat()
                forResult(listener)
            }
    }

    //调用图片选择器 多选图片
    fun addMultiplePicture(activity: Activity, maxImages: Int, selectList: List<LocalMedia?>, listener: OnResultCallbackListener<LocalMedia>) {
        PictureSelector.create(activity)
            .openGallery(PictureMimeType.ofImage()) // 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
            .defaultSetting(activity).apply {
                maxSelectNum(maxImages) // 最大图片选择数量
                minSelectNum(1) // 最小选择数量
                selectionMode(PictureConfig.MULTIPLE) // 多选 or 单选
                selectionData(selectList) // 是否传入已选图片
                imgFormat()
                forResult(listener)//forResult(PictureConfig.CHOOSE_REQUEST) //结果回 调onActivityResult code
            }

    }

    fun addCameraPicture(activity: Activity, listener: OnResultCallbackListener<LocalMedia>) {
        PictureSelector.create(activity)
            .openCamera(PictureMimeType.ofImage())
            .isCompress(true) // 是否压缩
            .minimumCompressSize(500)
            .imgFormat()
            .forResult(listener)

    }

    /*预览图片*/
    fun previewPicture(activity: Activity, position: Int, selectList: List<LocalMedia>?) {
        PictureSelector.create(activity)
            .themeStyle(R.style.picture_default_style) // xml设置主题
            .setPictureStyle(PictureParameterStyle()) // 动态自定义相册主题
            .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) // 设置相册Activity方向，不设置默认使用系统
            .imageEngine(YcGlideEngine.createGlideEngine()) // 外部传入图片加载引擎，必传项
            .openExternalPreview(position, selectList)

    }


    private fun PictureSelectionModel.imgFormat(): PictureSelectionModel {
        //适配android10和11的拍照闪退
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
            imageFormat(PictureMimeType.PNG_Q)
        else
            imageFormat(PictureMimeType.PNG)
        return this
    }

    private fun PictureSelectionModel.defaultSetting(activity: Activity): PictureSelectionModel {
        imageEngine(YcGlideEngine.createGlideEngine())//外部传入图片加载引擎，必传项
        setLanguage(LanguageConfig.CHINESE)
        isCompress(true)// 是否压缩
        minimumCompressSize(500)// 小于500kb的图片不压缩
        compressQuality(60)// 图片压缩后输出质量
        //theme(R.style.picture_default_style) // 主题样式设置 具体参考 values/styles // 用法：R.style.picture.white.style v2.3.3后 建议使用setPictureStyle()动态方式
        setPictureStyle(getStyle(activity)) // 动态自定义相册主题
        isWeChatStyle(true) // 是否开启微信图片选择风格
        isReturnEmpty(true) // 未选择数据时点击按钮是否可以返回
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) // 设置相册Activity方向，不设置默认使用系统
        isPreviewImage(true) // 是否可预览图片
        isZoomAnim(true) // 图片列表点击 缩放效果 默认true
        isPreviewEggs(true) // 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中)
        //.cropCompressQuality(90)// 注：已废弃 改用cutOutQuality()
        cutOutQuality(90) // 裁剪输出质量 默认100
        rotateEnabled(false)// 裁剪是否可旋转图片
        return this
    }

    fun getStyle(context: Context): PictureParameterStyle {
        // 相册主题
        val mPictureParameterStyle = PictureParameterStyle()
        // 是否改变状态栏字体颜色(黑白切换)
        mPictureParameterStyle.isChangeStatusBarFontColor = false
        // 是否开启右下角已完成(0/9)风格
        mPictureParameterStyle.isOpenCompletedNumStyle = false
        // 是否开启类似QQ相册带数字选择风格
        mPictureParameterStyle.isOpenCheckNumStyle = true
        // 状态栏背景色
        mPictureParameterStyle.pictureStatusBarColor = Color.parseColor("#393a3e")
        // 相册列表标题栏背景色
        mPictureParameterStyle.pictureTitleBarBackgroundColor = Color.parseColor("#393a3e")
        // 相册父容器背景色
        mPictureParameterStyle.pictureContainerBackgroundColor = ContextCompat.getColor(context, R.color.picture_color_black)
        // 相册列表标题栏右侧上拉箭头
        mPictureParameterStyle.pictureTitleUpResId = R.drawable.picture_icon_wechat_up
        // 相册列表标题栏右侧下拉箭头
        mPictureParameterStyle.pictureTitleDownResId = R.drawable.picture_icon_wechat_down
        // 相册文件夹列表选中圆点
        mPictureParameterStyle.pictureFolderCheckedDotStyle = R.drawable.picture_orange_oval
        // 相册返回箭头
        mPictureParameterStyle.pictureLeftBackIcon = R.drawable.picture_icon_close
        // 标题栏字体颜色
        mPictureParameterStyle.pictureTitleTextColor = ContextCompat.getColor(context, R.color.picture_color_white)
        // 相册右侧按钮字体颜色  废弃 改用.pictureRightDefaultTextColor和.pictureRightDefaultTextColor
        mPictureParameterStyle.pictureCancelTextColor = ContextCompat.getColor(context, R.color.picture_color_53575e)
        // 相册右侧按钮字体默认颜色
        mPictureParameterStyle.pictureRightDefaultTextColor = ContextCompat.getColor(context, R.color.picture_color_53575e)
        // 相册右侧按可点击字体颜色,只针对isWeChatStyle 为true时有效果
        mPictureParameterStyle.pictureRightSelectedTextColor = ContextCompat.getColor(context, R.color.picture_color_white)
        // 相册右侧按钮背景样式,只针对isWeChatStyle 为true时有效果
        mPictureParameterStyle.pictureUnCompleteBackgroundStyle = R.drawable.picture_send_button_default_bg
        // 相册右侧按钮可点击背景样式,只针对isWeChatStyle 为true时有效果
        mPictureParameterStyle.pictureCompleteBackgroundStyle = R.drawable.picture_send_button_bg
        // 相册列表勾选图片样式
        mPictureParameterStyle.pictureCheckedStyle = R.drawable.picture_wechat_num_selector
        // 相册标题背景样式 ,只针对isWeChatStyle 为true时有效果
        mPictureParameterStyle.pictureWeChatTitleBackgroundStyle = R.drawable.picture_album_bg
        // 微信样式 预览右下角样式 ,只针对isWeChatStyle 为true时有效果
        mPictureParameterStyle.pictureWeChatChooseStyle = R.drawable.picture_wechat_select_cb
        // 相册返回箭头 ,只针对isWeChatStyle 为true时有效果
        mPictureParameterStyle.pictureWeChatLeftBackStyle = R.drawable.picture_icon_back
        // 相册列表底部背景色
        mPictureParameterStyle.pictureBottomBgColor = ContextCompat.getColor(context, R.color.picture_color_grey)
        // 已选数量圆点背景样式
        mPictureParameterStyle.pictureCheckNumBgStyle = R.drawable.picture_num_oval
        // 相册列表底下预览文字色值(预览按钮可点击时的色值)
        mPictureParameterStyle.picturePreviewTextColor = ContextCompat.getColor(context, R.color.picture_color_white)
        // 相册列表底下不可预览文字色值(预览按钮不可点击时的色值)
        mPictureParameterStyle.pictureUnPreviewTextColor = ContextCompat.getColor(context, R.color.picture_color_9b)
        // 相册列表已完成色值(已完成 可点击色值)
        mPictureParameterStyle.pictureCompleteTextColor = ContextCompat.getColor(context, R.color.picture_color_white)
        // 相册列表未完成色值(请选择 不可点击色值)
        mPictureParameterStyle.pictureUnCompleteTextColor = ContextCompat.getColor(context, R.color.picture_color_53575e)
        // 预览界面底部背景色
        mPictureParameterStyle.picturePreviewBottomBgColor = ContextCompat.getColor(context, R.color.picture_color_half_grey)
        // 外部预览界面删除按钮样式
        mPictureParameterStyle.pictureExternalPreviewDeleteStyle = R.drawable.picture_icon_delete
        // 原图按钮勾选样式  需设置.isOriginalImageControl(true); 才有效
        mPictureParameterStyle.pictureOriginalControlStyle = R.drawable.picture_original_wechat_checkbox
        // 原图文字颜色 需设置.isOriginalImageControl(true); 才有效
        mPictureParameterStyle.pictureOriginalFontColor = ContextCompat.getColor(context, R.color.white)
        // 外部预览界面是否显示删除按钮
        mPictureParameterStyle.pictureExternalPreviewGonePreviewDelete = true
        // 设置NavBar Color SDK Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP有效
        mPictureParameterStyle.pictureNavBarColor = Color.parseColor("#393a3e")
        return mPictureParameterStyle
    }
}